package com.example.androidlearning2200.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidlearning2200.R;
import com.example.androidlearning2200.bean.NewsBean;

import java.util.List;

public abstract class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.RVHolder> {

    private List<NewsBean> datas;

    public abstract void clickItem(int position);

    public NewsAdapter(List<NewsBean> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_news, parent, false);
        return new RVHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RVHolder holder, int position) {
        NewsBean newsBean = datas.get(position);
        holder.tvTitle.setText(newsBean.getTitle());
        holder.tvAuthor.setText(newsBean.getAuthor());
        holder.tvCommentCount.setText(newsBean.getCommentCounts() + "评论");
        holder.tvPublishTime.setText(newsBean.getPublishTime());
        holder.ivPic.setImageResource(newsBean.getResId());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class RVHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private TextView tvPublishTime;
        private TextView tvCommentCount;
        private TextView tvAuthor;
        private ImageView ivPic;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvPublishTime = itemView.findViewById(R.id.tvPublishTime);
            tvCommentCount = itemView.findViewById(R.id.tvCommentCount);
            tvAuthor = itemView.findViewById(R.id.tvAuthor);
            ivPic = itemView.findViewById(R.id.ivPic);
        }

    }

}
